package com.leyu.mall.admin.cms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.leyu.mall.common.model.NewsModel;
import com.leyu.mall.domain.cms.CmsChannel;
import com.leyu.mall.domain.cms.CmsNews;
import com.leyu.mall.domain.cms.CmsNewsType;
import com.leyu.mall.service.cms.ICmsChannelService;
import com.leyu.mall.service.cms.ICmsNewsService;
import com.leyu.mall.service.cms.ICmsNewsTypeService;
/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * @author OOKK
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@Scope(value = "singleton")
@RequestMapping("admin/news")
public class CmsNewsController
{
	@Autowired
	private ICmsChannelService	cmsChannelService;
	@Resource(name = "cmsNewsServiceImpl")
	private ICmsNewsService		cmsNewsServiceImpl;

	
	@Resource 
	private ICmsNewsTypeService		cmsNewsTypeServiceImpl;
	
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Model model, CmsNews cmsNews)
	{
		try
		{
			cmsNewsServiceImpl.save(cmsNews);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../news";
	}

	@RequestMapping(value = "delete/{newsId}", method = RequestMethod.GET)
	public String delete(Model model, @PathVariable long newsId)
	{
		try
		{
			cmsNewsServiceImpl.deleteCmsNews(newsId);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../../news";
	}

	@RequestMapping(value = "edit/{newsId}", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable long newsId)
	{
		try
		{
			model.addAttribute("newstypelist", cmsNewsTypeServiceImpl.getCmsNewsTypeList());
//			model.addAttribute("channellist", cmsChannelService.getcmsChannelList());
			model.addAttribute("entity", cmsNewsServiceImpl.getCmsNewsById(newsId));
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/editnews.html";
	}

	@ExceptionHandler(Exception.class)
	public String exception(Exception e, HttpServletRequest request)
	{
		request.setAttribute("exception", e);
		return "/error.html";
	}

	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String newNews(Model model) throws Exception
	{
		
		model.addAttribute("channellist", cmsChannelService.getcmsChannelList());
		model.addAttribute("newstypelist", cmsNewsTypeServiceImpl.getCmsNewsTypeList());
		return "admin/newnews.html";
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String index(Model model,@RequestParam(value = "curPage", required = false, defaultValue ="0" ) int curPage) throws Exception
	{
		try
		{
			List<NewsModel> newsmodelList = new ArrayList();
			CmsNews pagebean = new CmsNews();
			int totalItem = cmsNewsServiceImpl.getNewsListByPageCount();
			pagebean.setTotalItem(totalItem);
			pagebean.setCurPage(curPage);
			
			List<CmsNews> cmsnewsList = cmsNewsServiceImpl.getNewsListByPage(pagebean);
			System.out.println("+++++++++++++++++++++++11111111---" + cmsnewsList.size());
			List<CmsNewsType> newsTypeList = cmsNewsTypeServiceImpl.getCmsNewsTypeList();
			for (CmsNews cmsnews : cmsnewsList)
			{
				for (CmsNewsType newsType : newsTypeList)
				{
					if (cmsnews.getNewsTypeId() == newsType.getNewsTypeId())
					{
						NewsModel nm = new NewsModel();
						System.out.println("+++++++++++++++++++++++" + cmsnews.getNewsTitle());
						nm.setChannelId(cmsnews.getChannelId());
						nm.setCreateTime(cmsnews.getCreateTime());
						nm.setNewsBody(cmsnews.getNewsBody());
						nm.setNewsId(cmsnews.getNewsId());
						nm.setNewsTitle(cmsnews.getNewsTitle());
						nm.setNewsTypeTitle(newsType.getNewsTypeName());
						newsmodelList.add(nm);
					}
				}
			}
			model.addAttribute("list", newsmodelList);
			model.addAttribute("pagebean", pagebean);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/news.html";
	}
}
